﻿using System;
using System.Html.DOM;

namespace RexToy.UI.Controls
{
    public class CheckBox : Control, IChoiceControl
    {
        private bool __checked;
        public bool Checked
        {
            get { return __checked; }
            set
            {
                this.__checked = value;
                var l = this.__domNode.SearchByName("lbl");
                var c = this.__domNode.SearchByName("check");
                if (this.__checked)
                {
                    if (this._activeOnCheck)
                        l.AppendClass("ui-btn-active");
                    c.ReplaceClass("ui-icon-checkbox-off", "ui-icon-checkbox-on");
                }
                else
                {
                    if (this._activeOnCheck)
                        l.RemoveClass("ui-btn-active");
                    c.ReplaceClass("ui-icon-checkbox-on", "ui-icon-checkbox-off");
                }
            }
        }


        private bool __enable;
        public bool Enable
        {
            get { return __enable; }
            set
            {
                if (value)
                {
                    this.__domNode.RemoveClass("ui-disabled");
                }
                else
                {
                    this.__domNode.AppendClass("ui-disabled");
                }

                this.__enable = value;
            }
        }

        public override bool Standalone
        {
            get
            {
                return __standalone;
            }
            set
            {
                var spn = this.__domNode.SearchByName("spn");
                var lbl = this.__domNode.SearchByName("lbl");
                if (value)
                {
                    spn.AppendClass("ui-corner-all");
                    lbl.AppendClass("ui-corner-all");
                }
                else
                {
                    spn.RemoveClass("ui-corner-all");
                    lbl.RemoveClass("ui-corner-all");
                }
                __standalone = value;
            }
        }

        private object __value;
        public object Value
        {
            get { return __value; }
        }

        private bool _activeOnCheck;

        public string[] events = new string[] { "onchange" };

        public CheckBox(string text, object value, bool activeOnCheck)
        {
            __enable = true;
            this.__value = value;
            this._activeOnCheck = activeOnCheck;
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Div,
                className = "ui-checkbox",
                items = new DomNodeCreateOptions[]{
                new DomNodeCreateOptions()
                {
                    tag= Tag.Input,
                    type= InputType.CheckBox
                },
                new DomNodeCreateOptions()
                {
                    tag= Tag.Label,
                    hashName= "lbl",
                    className= "ui-btn ui-btn-icon-left ui-corner-all ui-btn-up-c",
                    items= new DomNodeCreateOptions[]{
                        new DomNodeCreateOptions()
                        {
                            tag= Tag.Span,
                            hashName= "spn",
                            className= "ui-btn-inner ui-corner-all",
                            items= new DomNodeCreateOptions[]{
                                new DomNodeCreateOptions(){
                                    tag= Tag.Span,
                                    className= "ui-btn-text",
                                    innerHTML= text
                                },
                                new DomNodeCreateOptions(){
                                    tag= Tag.Span,
                                    hashName= "check",
                                    className= "ui-icon ui-icon-checkbox-off"
                                }
                            }
                        }
                    }
                }
            }
            });
            this.__domNode.AttachEvent(DomEvent.OnClick, new Action(this.OnClick).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnDoubleClick, new Action(this.OnClick).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOver, new Action(this.OnMouseOver).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOut, new Action(this.OnMouseOut).Bind(this));
        }

        private void OnMouseOver()
        {
            if (this.__enable == true)
            {
                var l = this.__domNode.SearchByName("lbl");
                l.ReplaceClass("ui-btn-up-c", "ui-btn-hover-c");
            }
        }

        private void OnMouseOut()
        {
            if (this.__enable)
            {
                var l = this.__domNode.SearchByName("lbl");
                l.ReplaceClass("ui-btn-hover-c", "ui-btn-up-c");
            }
        }

        private void OnClick()
        {
            if (this.__enable)
            {
                this.Checked = !this.__checked;

                this.__FireEvent("onchange");
            }
        }

        public override void SetFirst(bool first)
        {
            var spn = this.__domNode.SearchByName("spn");
            var lbl = this.__domNode.SearchByName("lbl");
            if (first == true)
            {
                spn.AppendClass("ui-corner-top");
                lbl.AppendClass("ui-corner-top");
            }
            if (first == false)
            {
                spn.RemoveClass("ui-corner-top");
                lbl.RemoveClass("ui-corner-top");
            }
        }

        public override void SetLast(bool last)
        {
            var spn = this.__domNode.SearchByName("spn");
            var lbl = this.__domNode.SearchByName("lbl");
            if (last == true)
            {
                spn.AppendClass("ui-corner-bottom");
                lbl.AppendClass("ui-corner-bottom");
            }
            if (last == false)
            {
                spn.RemoveClass("ui-corner-bottom");
                lbl.RemoveClass("ui-corner-bottom");
            }
        }
    }
}
